package com.fcscs.cservice.client.main.configuration.inroomdining;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeType;
import com.fcscs.cservice.client.common.model.reservation.PaymentMethodModelData;

public class ChargeTypeDialog extends BaseFormDialog<ChargeType> {

	private static ChargeTypeDialog INSTANCE;
	private MyFormPanel myFormPanel;
	private HorizontalPanel hp1;
	private LayoutContainer lc1;
	private TextField<String> chargeTypeField;
	private HorizontalPanel hp2;
	private LayoutContainer lc2;
	private TextField<String> codeField;
	private HorizontalPanel hp3;
	private LayoutContainer lc3;
	private EasyClearCombobox<PaymentMethodModelData> transCombox;
	private ListStore<PaymentMethodModelData> revenueStore;
	private FormButton saveBtn;
	private FormButton cancelBtn;
	private boolean isAdd;
	
	private HorizontalPanel hp4;
	private LayoutContainer lc4;
	private CheckBox isServiceBox;
	private HorizontalPanel hp5;
	private LayoutContainer lc5;
	private TextField<String> englishField;
	private HorizontalPanel hp6;
	private LayoutContainer lc6;
	private TextField<String> simpleField;
	
	private HorizontalPanel hp7;
	private LayoutContainer lc7;
	private TextField<String> triField;
	
	
	public static ChargeTypeDialog getInstance(boolean add) {

		if (null == INSTANCE) {
			INSTANCE = new ChargeTypeDialog();
		}
		INSTANCE.isAdd = add;
		INSTANCE.setHeader();
		INSTANCE.clear();
		INSTANCE.setHeading(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.add(),
				CServiceConstants.CSERVICE_CONSTANTS.chargeType()));
		return INSTANCE;
	}
	
	public void setHeader(){
		if (isAdd) {
			INSTANCE.setHeading(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.add(),
					CServiceConstants.CSERVICE_CONSTANTS.chargeType()));
		} else {
//			INSTANCE.updatePanel();
			INSTANCE.setHeading(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.edit(),
					CServiceConstants.CSERVICE_CONSTANTS.chargeType()));
		}   
	}

	private ChargeTypeDialog() {
		myFormPanel = new MyFormPanel(null);
		add(myFormPanel);
		setSize(450, 300);
	}

	public class MyFormPanel extends FcsFormPanel<ChargeType> {

		protected MyFormPanel(FormSubmitListener<ChargeType> l) {
			super(l);
			setHeaderVisible(false);

			VerticalPanel vp = new VerticalPanel();
			vp.setSpacing(5);
			vp.setId("ChargeTypeDialog_vp");

			hp1 = new HorizontalPanel();
			hp1.setId("ChargeTypeDialog_hp1");
			vp.add(hp1);

			lc1 = new LayoutContainer();
			lc1.setId("ChargeTypeDialog_lc1");
			lc1.setLayout(new ColumnLayout());
			hp1.add(lc1);
			Label chargeTypeLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.chargeType()
					+ CServiceConstants.FIELD_SEP);
			chargeTypeLabel.setId("ChargeTypeDialog_chargeTypeLabel");
			lc1.add(chargeTypeLabel, new ColumnData(150));

			chargeTypeField = new TextField<String>();
			chargeTypeField.setId("ChargeTypeDialog_chargeTypeLabel");
			lc1.add(chargeTypeField, new ColumnData(200));

			hp2 = new HorizontalPanel();
			hp2.setId("ChargeTypeDialog_hp2");
			vp.add(hp2);

			lc2 = new LayoutContainer();
			lc2.setId("ChargeTypeDialog_lc2");
			lc2.setLayout(new ColumnLayout());
			hp2.add(lc2);

			Label codeLabel = new Label(CoreConstants.CORE_CONSTANTS.code() + CServiceConstants.FIELD_SEP);
			codeLabel.setId("ChargeTypeDialog_codeLabel");
			lc2.add(codeLabel, new ColumnData(150));

			codeField = new TextField<String>();
			codeField.setId("ChargeTypeDialog_codeField");
			lc2.add(codeField, new ColumnData(200));

			hp3 = new HorizontalPanel();
			hp3.setId("ChargeTypeDialog_hp3");
			vp.add(hp3);

			lc3 = new LayoutContainer();
			lc3.setId("ChargeTypeDialog_lc3");
			lc3.setLayout(new ColumnLayout());
			hp3.add(lc3);

			Label transLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.transactionType()
					+ CServiceConstants.FIELD_SEP);
			transLabel.setId("ChargeTypeDialog_transLabel");
			lc3.add(transLabel, new ColumnData(150));

			transCombox = new EasyClearCombobox<PaymentMethodModelData>();
			transCombox.setId("ChargeTypeDialog_revenueCombox");
			revenueStore = new ListStore<PaymentMethodModelData>();
			transCombox.setStore(revenueStore);
			transCombox.setDisplayField(CoreConstants.FULLTNAME);
			transCombox.setTriggerAction(TriggerAction.ALL);
			transCombox.setForceSelection(true);
			lc3.add(transCombox, new ColumnData(200));
			
			hp4 = new HorizontalPanel();
			hp4.setId("ChargeTypeDialog_hp4");
			vp.add(hp4);

			lc4 = new LayoutContainer();
			lc4.setId("ChargeTypeDialog_lc4");
			lc4.setLayout(new ColumnLayout());
			hp4.add(lc4);
			Label isServiceChargeLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.isServiceCharge()
					+ CServiceConstants.FIELD_SEP);
			isServiceChargeLabel.setId("ChargeTypeDialog_isServiceCharge");
			lc4.add(isServiceChargeLabel, new ColumnData(150));

			isServiceBox = new CheckBox();
			isServiceBox.setId("ChargeCategoryDialog_isServiceBox");
			lc4.add(isServiceBox);
			
			hp5 = new HorizontalPanel();
			hp5.setId("ChargeTypeDialog_hp5");
			vp.add(hp5);

			lc5 = new LayoutContainer();
			lc5.setId("ChargeTypeDialog_lc5");
			lc5.setLayout(new ColumnLayout());
			hp5.add(lc5);
			Label englishLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.english()
					+ CServiceConstants.FIELD_SEP);
			englishLabel.setId("ChargeTypeDialog_englishLabel");
			lc5.add(englishLabel, new ColumnData(150));

			englishField = new TextField<String>();
			englishField.setId("ChargeTypeDialog_englishLabel");
			lc5.add(englishField, new ColumnData(200));
			
			hp6 = new HorizontalPanel();
			hp6.setId("ChargeTypeDialog_hp6");
			vp.add(hp6);

			lc6 = new LayoutContainer();
			lc6.setId("ChargeTypeDialog_lc6");
			lc6.setLayout(new ColumnLayout());
			hp6.add(lc6);
			Label simpleLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.simplifiedChinese()
					+ CServiceConstants.FIELD_SEP);
			simpleLabel.setId("ChargeTypeDialog_simpleLabel");
			lc6.add(simpleLabel, new ColumnData(150));

			simpleField = new TextField<String>();
			simpleField.setId("ChargeTypeDialog_simpleField");
			lc6.add(simpleField, new ColumnData(200));
			
			hp7 = new HorizontalPanel();
			hp7.setId("ChargeTypeDialog_hp7");
			vp.add(hp7);

			lc7 = new LayoutContainer();
			lc7.setId("ChargeTypeDialog_lc7");
			lc7.setLayout(new ColumnLayout());
			hp7.add(lc7);
			Label tridiLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.triditionalChinese()
					+ CServiceConstants.FIELD_SEP);
			tridiLabel.setId("ChargeTypeDialog_tridiLabel");
			lc7.add(tridiLabel, new ColumnData(150));

			triField = new TextField<String>();
			triField.setId("ChargeTypeDialog_triField");
			lc7.add(triField, new ColumnData(200));
			
			saveBtn = new FormButton(CoreConstants.CORE_CONSTANTS.save());
			saveBtn.setId("ChargeTypeDialog_saveBtn_Id");
			getButtonBar().add(saveBtn);

			cancelBtn = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
			cancelBtn.setId("ChargeTypeDialog_cancelBtn_Id");
			cancelBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					ChargeTypeDialog.this.hide();
					cancelBtn.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
				}
			});
			getButtonBar().add(cancelBtn);

			add(vp);

		}

		@Override
		public void setFocus() {

		}

		@Override
		public void updateForm(ChargeType entity) {
			chargeTypeField.setValue(entity.getChargetypeStr());
			codeField.setValue(entity.getCode());
			transCombox.setRawValue("tx");
		}

		@Override
		public void populateEntity(ChargeType entity) {

		}

	}

	@Override
	public void hide() {
		super.hide();
	}
	
	public void clear(){
		chargeTypeField.clear();
		codeField.clear();
		transCombox.clear();
	}

	@Override
	protected FcsFormPanel<ChargeType> getFormPanel() {
		return myFormPanel;
	}

}
